<?php

//编辑器上传图片处理
class Myupload extends Controller {
	
	function Myupload()
	{
		parent::Controller();	
	}
	
	function index()
	{
		if ($dosubmit = p("dosubmit")) {
			//图片上传
			$pic = $this->upload("myfile");
			//图片裁剪
			if($pic != "") {//图片剪切
				$params = array('src_file' => './data/upload_pic/'.$pic, 'dst_file' => './data/upload_pic/'.$pic);
				$this->load->library("imagecrop", $params);
				$this->imagecrop->Crop(300,300,1);
				$this->imagecrop->SaveImage();
				$this->imagecrop->destory();
			} else {
				echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
				echo '<script language="javascript">
				alert("图片上传失败");
				self.parent.tb_remove();
				</script>';
				exit;
			}

			echo '<script language="javascript">
			var content = parent.document.getElementById("myedcontent").innerHTML;
			var myimg = "<img src=/data/upload_pic/'.$pic.' />";
			content = content+myimg;
			parent.document.getElementById("myedcontent").innerHTML = content;
			self.parent.tb_remove();
			</script>';
			exit;
		}
		$this->load->view(__CLASS__."/index");
	}
	
	//上传处理
	function upload($filename)
	{
		$config['upload_path'] = './data/upload_pic';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = strtotime(date('Y-m-d H:i:s')).mt_rand(1,1000);//加上随机数
		$config['max_size'] = '3072';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);//初始化上传类
		$pic = "";
		if ($this->upload->do_upload($filename)) {
			$updata = $this->upload->data();
			$pic = $updata["file_name"];
		} 
		return $pic;
	}
}

/* End of file */